package cn.itcast.service.system.impl;

import cn.itcast.dao.system.UserDao;
import cn.itcast.domain.system.User;
import cn.itcast.service.system.UserService;
import cn.itcast.utils.MailUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;

    @Autowired
    private AmqpTemplate amqpTemplate;

    @Override
    public List<User> findAll(String companyId) {
        return userDao.findAll(companyId);
    }

    @Override
    public void save(User user) {
//        String password_mingwen = user.getPassword();
        String password_random = RandomStringUtils.random(6, "utf-8");

//        Object source 原密码, Object salt, int hashIterations 散次列 加几次
        String password_miwen = new Md5Hash(password_random, user.getEmail(), 2).toString();
        user.setPassword(password_miwen);
        userDao.save(user);

//        MailUtil.sendMsg(user.getEmail(),"恭喜您成为SaaS货代云平台的会员","您的登录密码是"+password_random);
//        发送邮件 发送密码 // TODO 改成邮件发送
        Map<String,String> map = new HashMap<>();
        map.put("to",user.getEmail());
        map.put("subject","恭喜您成为SaaS货代云平台的会员");
        map.put("content","您的登录密码是"+password_random);

        amqpTemplate.convertAndSend("user.email",map);
    }

    @Override
    public User findById(String id) {
        return userDao.findById(id);
    }

    @Override
    public void update(User user) {
        userDao.updateById(user);
    }

    @Override
    public void deleteById(String id) {
        userDao.deleteById(id);
    }

    @Override
    public PageInfo findPage(String companyId,Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<User> list = userDao.findAll(companyId);
        return new PageInfo<>(list,5);

    }

    @Override
    @Transactional
    public void changeRole(String userid, String[] roleIds) {

//        1、先删除这个用户之前的所有角色
        userDao.deleteUserAndRoleByUserId(userid);

//        2、再重新保存这个用户现在的角色
        for (String roleId : roleIds) {
//            insert into pe_role_user (user_id,role_id) values (#{userid},#{roleid});
            userDao.saveUserAndRole(userid,roleId);
        }
    }

    @Override
    public List<String> findRoleIdsByUserId(String id) {
        return userDao.findRoleIdsByUserId(id);
    }

    @Override
    public User findByEmail(String email) {
        return userDao.findByEmail(email);
    }

    public static void main(String[] args) {
//        Object source 原密码, Object salt, int hashIterations 散次列 加几次
//        String password = new Md5Hash("123456", "laowang@export.com", 2).toString();
        String password = new Md5Hash("123456", "123456789@qq.com", 2).toString();
        System.out.println(password);
    }
}
